<?php
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\Log\Logger;

$category = $response->getAttribute("object");
$objectType = $response->getAttribute("objectType");
?>
<?php 
if (!is_null($category)) {
    ?>
<div class="preview">
    <input
        type="button"
        value="Preview"
        onclick="javascript:window.open(
            '<?php Printer::display(HTML::linkHref('action=ShowProfile&category_id='.$category->getId()));?>',
            'preview',
            'width=1024,
            height=768'
        )"
    />
</div>
<?php 
}
?>
<form
    class="editForm"
    method="POST"
    action="<?php Printer::display(HTML::linkHref('action=CMS.Save&type='.$objectType));?>">
    <?php
    if ($category != null) {
    ?>
    <input
        type="hidden"
        name="id"
        value="<?php Printer::display($category->getId()); ?>" />
    <?php
    }
    ?>
    <div class="label">
        <span class="titre">Nom :</span>
        <input
            class="firstInput"
            type="text"
            name="name"
            value="<?php if ($category != null) Printer::display($category->getName()); ?>" />
    </div>
    <div class="label" id="profile">
        <span class="titre">Fiche :</span>
        <input
            class="firstInput fieldTypeSelecter"
            type="text"
            id="profile_name"
            name="profile_name"
            value="<?php
                if ($category != null && $category->getProfile()!=null) {
                    Printer::display($category->getProfile()->getName());
                } 
            ?>" />
        <input
            class="firstInput inputId"
            type="hidden"
            id="profile_id"
            name="profile_id"
            value="<?php
                if ($category != null && $category->getProfile()!=null) {
                    Printer::display($category->getProfile()->getId()); 
                }
            ?>" />
    </div>
    <div class="label" id="style">
        <span class="titre">Style :</span>
        <input
            class="firstInput fieldTypeSelecter"
            type="text"
            id="style_name"
            name="style_name"
            value="<?php
                if ($category != null && $category->getStyle()!=null) {
                    Printer::display($category->getStyle()->getName());
                } 
            ?>" />
        <input
            class="firstInput inputId"
            type="hidden"
            id="style_id"
            name="style_id"
            value="<?php
                if ($category != null && $category->getStyle()!=null) {
                    Printer::display($category->getStyle()->getId());
                }
            ?>" />
    </div>
    <div class="label" id="style">
        <span class="titre">Sites :</span>
        <div class="formPartsOrderer" id="sites">
            <ul class="formPartsList">
                <?php 
                if ($category != null) {
                    $logger = new Logger("profile");
                    $sites = $category->getSites();

                    if (!is_null($category) && !is_null($sites)) {
                        for ($i=0; $i<count($sites); $i++) {
                            $part = $sites[$i] ;
                            $site = $part->getSite();
                            if (!is_null($site)) {
                ?>
                <li>
                    <span class="item">
                        <?php Printer::display($site->getName()); ?>
                        - 
                        <input
                            class="id"
                            type="hidden"
                            name="part_id[]"
                            value="<?php Printer::display($site->getId()); ?>" />
                        <label>
                            Id sur le site : 
                            <input
                                type="text"
                                name="id_for_site[]"
                                size="2"
                                value="<?php Printer::display($part->getIdForSite()); ?>" />
                        </label>
                        <input
                            class="order"
                            type="hidden"
                            name="part_order[]"
                            value="<?php Printer::display($i+1); ?>" />
                    </span>
                </li>
                <?php 
                            }
                        }
                    }
                }
                ?>
            </ul>
        </div>
    </div>
    <input type="submit" value="OK" />
</form>
<script type="text/javascript">
    $(document).ready(function() {
        $("#sites .formPartsList").formPartsOrderer(
            {
                "serviceUrl":"?service=ListSites&format=json",
                "container":"#sites",
                "fields":[
                      {
                          "type":"text",
                          "name":"id_for_site",
                          "label":"Id sur le site",
                          "size":"2"
                      }
                  ]
            }
        );

        $("#profile_name").autocomplete({
            source: "?service=ListProfiles&format=json",
            select: function(event, ui) {
                $("#profile_name").val(ui.item.label);
                $("#profile_id").val(ui.item.id);
            }
        });

        $("#style_name").autocomplete({
            source: "?service=ListStyles&format=json",
            select: function(event, ui) {
                $("#style_name").val(ui.item.label);
                $("#style_id").val(ui.item.id);
            }
        });
    });
</script>
